﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using LinFu.AOP.Interfaces;
using LinFu.IoC.Configuration;

namespace CoreEx.Database.Client.Tests
{
    [Implements(typeof(IProfiler<IDbCommand>), LifecycleType.OncePerThread)]
    public class SampleCommandProfiler : IProfiler<IDbCommand>
    {
        private Stopwatch _stopwatch;

        public long TimeElapsed { get; set; }
        
        public void BeforeInvoke(IInvocationInfo info)
        {
            if (info.TargetMethod.Name == "ExecuteReader")
            {                                
                _stopwatch = new Stopwatch();
                _stopwatch.Start();              
            }
        }

        public void AfterInvoke(IInvocationInfo info, object returnValue)
        {
            if (info.TargetMethod.Name == "ExecuteReader")
            {
                _stopwatch.Stop();
                TimeElapsed = _stopwatch.ElapsedMilliseconds;
                Console.WriteLine("Statement {0} took {1} milliseconds ",((IDbCommand)info.Target).CommandText,TimeElapsed);
            }
        }
    }
}
